﻿using BBCore.Core.Models.Admins;
using BootstrapBlazor.Components;

namespace BBCore.Server.Components.Pages.Admins
{
    public partial class Menu
    {
        private List<TreeViewItem<int>> Items { get; set; } = new List<TreeViewItem<int>>();
        private static bool ShowDetailRow(MenuEntity item) => !string.IsNullOrEmpty(item.CN_COLUMNS);
        protected override async Task OnInitializedAsync()
        {
            await base.OnInitializedAsync();
            await RefreshItems();
        }
        /// <summary>
        /// 刷新菜单数据
        /// </summary>
        /// <returns></returns>
        private async Task RefreshItems()
        {
            Items.Clear();
            var menus = await IAdminService.menu_MenuEntityListAsync();  
            Items.Add(new TreeViewItem<int>(0) { Text = "顶级菜单" });
            Items.AddRange(IAdminService.menu_TreeCascadingMenu(menus, 0));
        }
        private async Task<bool> OnSaveAsync(MenuEntity arg1, ItemChangedType arg2)
        {
            await arg1.SaveAsync();
            await RefreshItems();
            return true;
        }

        private async Task<bool> OnDeleteAsync(IEnumerable<MenuEntity> arg)
        {
            if (await DataService.DeleteAsync(arg))
            {
                await RefreshItems();
                return true;
            }
            return false;
        }
        private  Task<IEnumerable<TableTreeNode<MenuEntity>>> TreeNodeConverter(IEnumerable<MenuEntity> items)
        {
           var result= IAdminService.TreeNodeConverter(items);
            return result;
        }

    }
}
